/**
 * 
 */
package scriptrunner.execution;

import java.io.Serializable;

/**
 * @author Administrator
 * 
 */
public class EvalFuncExpr extends EvaluateExpr implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3581847540295006661L;

	public EvalFuncExpr(String id, ArgumentList al) {
		this.id = id;
		this.al = al;
		this.signature = new FuncSignature(id, al.getArgumentCount());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * scriptrunner.execution.EvaluateExpr#CalculateResult(scriptrunner.execution
	 * .Session)
	 */
	@Override
	public ValueObject CalculateResult(Session se) throws ExecuteErrorException {
		Session funcSe = new Session( se.getHttpParameter() );

		Function f = FunctionRegister.GetInstance().GetFunction(signature);

		if (f == null)
			throw new ExecuteErrorException("Cannot find function " +
					signature.GetName() +
					"(" + Integer.toString(signature.GetArgumentCount()) + ")");

		ParameterList pl = f.getParameterList();
		int paraCount = pl.getParameterCount();
		for (int i = 0; i < paraCount; ++i)
			funcSe.setVariableValueAsReference(pl.getParameterId(i), al.getArgument(i)
					.CalculateResult(se));
		//f.Execute(funcSe);
		//return funcSe.getReturnResult();
		return f.Execute(funcSe);
	}

	String id;
	ArgumentList al;
	FuncSignature signature;
}
